<template>
	<view>
		<js-error mode="bottom" ref="jsError"></js-error>
		<u-sticky class="u-sticky">
			<!-- bg-blue  -->
			<view class="padding-sm flex light " style="background-color: #eee;">
				<view style="width: 100%"><u-search placeholder="到货车次/车牌号/状态等" v-model="keyword" bg-color="#fff"
						:show-action="false" :disabled="true" @tap="show=true" searchIconSize="26"
						:inputStyle="inputStyle"></u-search></view>
				<!-- <u-icon @click="show=true" :name="filterIcon" size="60"
					class="margin-sm-left flex-sub margin-sm-right"></u-icon> -->

			</view>
		</u-sticky>
		<u-popup v-model="show" mode="right" length="90%">
			<view
				style="font-size: 18px;border-bottom: 1px solid #aaa;font-weight: bold;padding: 10px;display: flex;justify-content: space-between;">
				<!-- <u-icon name="/static/image/ss.png" size="65"></u-icon> -->
				<text class=" padding-left-sm" style="color: #3E97B0;">筛选</text>
				<text @click="show=false">关闭</text>
			</view>
			<!-- class="form"  -->
			<u-form class="form" style="padding: 0 10px;" :model="query" ref="uForm" label-position="left">
				<u-form-item class="text-bold" label="到货车次:" prop="djNo" label-width="230">
					<u-input placeholder="请输入" v-model="query.djNo" type="text" maxlength="200"></u-input>
				</u-form-item>
				<u-form-item class="text-bold" label="车牌号:" prop="carNo" label-width="230">
					<!-- <u-input placeholder="请输入" v-model="query.carNo" type="text" maxlength="200"></u-input> -->
					<xm-cell special label="" v-model="query.carNo" @show="showKeyboard('xmKeyboard')"></xm-cell>
					<!-- 虚拟键盘载体 -->
					<!-- (v) => model.carNo = v -->
					<xm-keyboard-v2 ref="xmKeyboard" @confirm="carNoconfirm"></xm-keyboard-v2>
				</u-form-item>
				<u-form-item class="text-bold" label="计划日期(起)" prop="time" label-width="230">
					<u-input placeholder="请输入" v-model="query.planDate_gte" type="select" :select-open="startTime"
						@click="startTime = true"></u-input>
				</u-form-item>
				<u-form-item class="text-bold" label="计划日期(止)" prop="time" label-width="230">
					<u-input placeholder="请输入" v-model="query.planDate_lte" type="select" :select-open="endTime" @click="endTime = true"></u-input>
				</u-form-item>
				<u-form-item class="text-bold" label="状态:" prop="cstatus" label-width="230">
					<js-select v-model="query.cstatus" dict-type="mf_plan_status" placeholder="请选择"></js-select>
				</u-form-item>
				<u-form-item class="text-bold" label="制单人:" prop="createByName" label-width="230">
					<u-input placeholder="请输入" v-model="query.createByName" type="text" maxlength="200"></u-input>
				</u-form-item>
				<u-picker mode="time" v-model="startTime" @confirm="startConfirm"></u-picker>
				<u-picker mode="time" v-model="endTime" @confirm="endConfirm"></u-picker>
			</u-form>
			<!-- <view class="footer">
	  		<u-button class="btn" type="primary" @click="submit">查询</u-button>
	  	</view> -->
			<view class=" flex cu-bar tabbar bg-white foot cu-modal-footer">
				<!-- round -->
				<button class="cu-btn  lines-red lg " @click="reset">重置</button>
				<button class="cu-btn  bg-confirm lg margin-left " @click="submit">查询</button>
			</view>
		</u-popup>
		<scroll-view scroll-y="true" @scrolltolower="loadMore" :refresher-enabled="true" :scrolltolower-enabled="true"
			:refresher-triggered="triggered" @refresherrefresh="refresherrefresh" @scrolltoupper="refresherrefresh"
			class="scroll-view-class" :style="{ height: computedScrollViewHeight }">
			<view class="scroll-content" ref="scrollContent" style="
          display: flex;
          flex-direction: column;
          justify-content: space-between;
        " :style="{ height: computedScrollViewHeight  }">
				<view v-for="(item,index) in list" class="cu-item shadow " style="position: relative;" :key="item.id">
					<view class="cu-form-group" style="display: flex;justify-content: space-between;">
						<view
							style=" display: inline-block;padding: 5px;background: #3E97B0;color: #fff;min-width: 40px;text-align: center;font-style: italic;font-weight: bold">
							{{ index + 1 }}
						</view>
						<view class="">
							<u-icon v-if="item.cstatus == '0' " @click="toEdit(item)" name="checkmark-circle"
								color="green" size="70" class="margin-sm-left flex-sub margin-sm-right"
								style="margin-left: 20px;"></u-icon>
							<u-icon v-if="item.cstatus!='3'" @click="toAdd(item)" name="plus-circle" color="#3E97B0"
								size="70" class="margin-sm-left flex-sub margin-sm-right"
								style="margin-left: 20px;"></u-icon>
							<u-icon v-if="item.cstatus!='3'  && item.cstatus!='4' " @click="addDH(item)" name="edit-pen" size="70"
								class="margin-sm-left flex-sub margin-sm-right" style="margin-left: 20px;"></u-icon>
							<u-icon v-if="item.cstatus!='3'  && item.cstatus!='4' " @click="delData(item.id)"
								name="trash" color="red" size="70" class="margin-sm-left flex-sub margin-sm-right"
								style="margin-left: 20px;"></u-icon>
						</view>
					</view>
					<!-- <view class="cu-form-group">
                <view class="text-xl text-bold">{{ item.starttime || "" }} <text style="margin: 0 10px;">至</text>  {{ item.endtime || "" }}</view>
              </view> -->
					<view class="cu-form-group  text-bold">
						<view class="title text-red">到货车次：</view>
						<view class="text-red" style="flex: 1;"> {{ item.djNo|| ""  }}</view>
						
						<view style="width: 70rpx;">
							<u-icon v-if="item.cstatus=='2'" @click="btnSdPush(item)" color="#3E97B0"  name="account" size="70"></u-icon>
						</view>
					</view>
					<view class="cu-form-group">
						<view class="title">车牌号：</view>
						<view style="flex: 1;"> {{ item.carNo|| ""  }}
						</view>
						<!-- <view style="width: 70rpx;">
							<u-icon @click="erpPush(item)" color="#3E97B0"  name="zhuanfa" size="70"></u-icon>
						</view> -->
						<view style="width: 70rpx;">
							<u-icon v-if="item.ipicture!='0'" @click="toFj(item)" name="camera" size="70"></u-icon>
						</view>
					</view>
					<view class="cu-form-group">
						<view class="title">计划日期：</view>
						<view style="flex: 1;"> {{ item.planDate|| ""  }} </view>
						<view style="width: 70rpx;">
							<u-icon v-if="item.cstatus=='1'" @click="btnOver(item)" color="#ffaa00" name="checkmark-circle" size="70"></u-icon>
						</view>
						
					</view>
					<view class="cu-form-group">
						<view class="title">制单人：</view>
						<view style="flex: 1;"> {{ item.creator?item.creator.createByName:''  }} </view>
						<view style="width: 70rpx;">
							<u-icon v-if="item.cstatus=='2'" @click="updateCstatus(item)" color="#3E97B0" name="reload" size="70"></u-icon>
						</view>
					</view>
					<view class="cu-form-group" style="color: #3E97B0;">
						<view class="title">状态：</view>
						<view style="flex: 1;"> {{ item.cstatusLabel|| ""  }} </view>
						
					</view>
					<!-- <view class="cu-form-group" style="color:#3E97B0 ;"  @click="add(item.id)">
                <view class="content-footer-right">查看详情</view>
                <u-icon name="arrow-right"></u-icon>
              </view> -->
					<view style="padding: 10px;background: #fff;">
						<view v-for="(items,indexs) in item.mfCarplanDhHList" :key="indexs"
							style="border: 1px solid #aaa;border-radius: 10px;overflow: hidden;position: relative;margin-bottom: 10px;">
							<view v-if="items.cstatus!='1'" style="position: absolute; right: 10px;bottom: 5px;display: flex;align-items: center;">
								<u-icon name="edit-pen" @click="editDetail(items)"   size="70"></u-icon>
								
								<text style="font-size: 35px;margin-left: 10px;" class="cuIcon-deletefill text-sl text-red"
									@tap="delDetail(items.id)"></text>
							</view>

							<view style="position: absolute; right: 10px;top: 10px;">
								<view
									style=" display: inline-block;padding: 5px;border: 1px solid #3E97B0;border-radius: 5px; color: #3E97B0;min-width: 30px;text-align: center;">
									{{indexs + 1}}
								</view>
							</view>


							<view class="cu-form-group text-red">
								<view class="title">订单号：</view>
								<view style="flex: 1;"> {{ items.poCode|| ""  }}</view>
							</view>
							<view class="cu-form-group">
								<view class="title">供应商：</view>
								<view style="flex: 1;"> {{ items.venName|| ""  }}
								</view>
							</view>
							<view class="cu-form-group">
								<view class="title">存货名称：</view>
								<view style="flex: 1;"> {{ items.invName|| ""  }}</view>
							</view>
							<view class="cu-form-group">
								<view class="title">规格型号：</view>
								<view style="flex: 1;"> {{ items.invStd|| ""  }}</view>
							</view>
							<view class="cu-form-group">
								<view class="title">合同号：</view>
								<view style="flex: 1;"> {{ items.contractCode|| ""  }}</view>
							</view>
							<view class="cu-form-group">
								<view class="title">计划重量：</view>
								<view style="flex: 1;"> {{ items.planWeight|| ""  }}</view>
							</view>
							<view class="cu-form-group">
								<view class="title">确认重量：</view>
								<view style="flex: 1;"> {{ items.qrWeight|| ""  }}</view>
							</view>
						</view>
					</view>
				</view>

				<view v-if="list.length" class="loadmore" style="justify-self: flex-end">
					<u-loadmore :status="loadStatus"></u-loadmore>
				</view>
				<u-empty v-if="!list.length" style="height: 80vh;"></u-empty>
			</view>
		</scroll-view>
		<!-- v-if="flag" -->
		<view class="btn-group cu-bar foot">
			<button class="cu-btn bg-green shadow-blur" @click="addDH({})">
				<text class="cuIcon-add"></text> 新增
			</button>
		</view>



		<u-popup mode="bottom" v-model="show2">
			<view class="content">
				<view  style="font-size: 18px;padding: 10px;">
					修改原因 （完成称重）:  
					<text >{{czData.djNo}}</text>
				</view>
				<view style="padding: 10px;">
					<textarea v-model="czData.remarks" placeholder="请输入" type="text" placeholder-class="line"
						style="width: 100%;" />
				</view>
				<view class="confrim-btn flex cu-bar tabbar cu-modal-footer">
					<button class="cu-btn  lines-red lg " @click="show2 = false">取消</button>
					<button class="cu-btn  bg-confirm lg margin-left " @click="czsubmit">确定</button>
				</view>
			</view>
		</u-popup>
	</view>
</template>
<script>
	import {
		hasPermission
	} from '@/common/fire.js'
	export default {
		data() {
			return {
				remarks: '',
				czData:{},
				show2: false,
				type: 'text',
				startTime: false, //控制日期显示
				endTime: false, //控制日期显示
				show: false,
				inputStyle: {
					fontSize: '34rpx'
				},
				filterIcon: "/static/image/filter.png",
				keyword: "",
				background: {
					backgroundColor: '#3E97B0',
				},
				list: [],
				itemContent: {},
				query: {
					pageNo: 1,
					pageSize: 5,
					// orderBy: "a.create_date desc",
				},
				loadStatus: "loadmore",
				triggered: false,
				// flag: hasPermission('app:proj:weekly:pmWeekly:edit'),
				flag: true,
				scrollViewHeight: 0,
				headerHeight: 0,
				projStatus: this.$store.state.auth.projStatus,
			};
		},
		onShow() {
			this.query.pageNo = 1;
			this.loadData();
		},
		onLoad(e) {

		},
		mounted() {
			this.calculateScrollViewHeight();
		},
		computed: {
			computedScrollViewHeight() {
				return this.scrollViewHeight === 'auto' ? 'auto' : `${this.scrollViewHeight}px`;
			},
		},
		methods: {
			carNoconfirm(v){
				this.query.carNo = v;
				this.$forceUpdate()
			},
			showKeyboard(ref){
				this.$refs[ref].toShow(this.query.carNo)
			},
			addDH(item) {
				const that = this
				let id = item.id?item.id:''
				uni.navigateTo({
					url: '/pages/mf/dh/dhdj?id=' + id,
					events: {
						// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
						Filter(data) {

						}
					},
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						// res.eventChannel.emit('initfilter', that.filter)
					}
				})
			},
			//跳转详情页面上传图片
			toFj(model) {
				const that = this
				uni.navigateTo({
					url: '/pages/mf/dh/dhfj?model=' + JSON.stringify(model),
					events: {
						// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
						Filter(data) {

						}
					},
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						// res.eventChannel.emit('initfilter', that.filter)
					}
				})
			},
			czsubmit(model) {
				this.show2= false
				let data = {
					djNo: this.czData.djNo,
					cstatus: 2,
					id: this.czData.id,
					remarks: this.czData.remarks
				}
				this.$u.api.mfsh.updateCzStatusByHand(data).then(res => {
					if (res.result == 'true') {
						this.$u.toast(res.message);
						this.loadData();
					} else {
						this.$refs.jsError.showError('',res.message,'error');
					}
				})
			},
			btnOver(model) {
				this.show2 = true
				this.czData = model
			},
			updateCstatus(model) {
				const that = this;
				uni.showModal({
					title: '修改提示',
					content: '确认二次称重吗？',
					confirmColor: '#00007f',
					success: (res) => {
						if (!res.confirm) {
							return false;
						} else {
							this.$u.api.mfsh.updateCstatus({
								id: model.id,
								cstatus:1
							}).then(res => {
								if (res.result == 'true') {
									that.$u.toast(res.message);
									that.loadData();
								} else {
									that.$refs.jsError.showError('',res.message,'error');
								}
							})
						}
					}
				})
			},
			btnSdPush(model) {
				const that = this;
				uni.showModal({
					title: '修改提示',
					content: '确认修改为推送状态吗？',
					confirmColor: '#00007f',
					success: (res) => {
						if (!res.confirm) {
							return false;
						} else {
							this.$u.api.mfsh.updatePushStatusByHand({
								id: model.id
							}).then(res => {
								if (res.result == 'true') {
									that.$u.toast(res.message);
									that.loadData();
								} else {
									that.$refs.jsError.showError('',res.message,'error');
								}
							})
						}
					}
				})
			},
			erpPush(model) {
				const that = this;
				uni.showModal({
					title: '修改提示',
					content: '确认推送ERP吗？',
					confirmColor: '#00007f',
					success: (res) => {
						if (!res.confirm) {
							return false;
						} else {
							this.$u.api.mfsh.updatePushStatusByHand({
								id: model.id
							}).then(res => {
								if (res.result == 'true') {
									that.$u.toast(res.message);
									that.loadData();
								} else {
									that.$refs.jsError.showError('',res.message,'error');
								}
							})
						}
					}
				})
			},
			toEdit(model) {
				const that = this;
				uni.showModal({
					title: '修改提示',
					content: '是否准备就绪？',
					confirmColor: '#00007f',
					success: (res) => {
						if (!res.confirm) {
							return false;
						} else {
							this.$u.api.mfsh.invalid({
								id: model.id
							}).then(res => {
								if (res.result == 'true') {
									that.$u.toast("成功！");
									that.loadData();
								} else {
									that.$refs.jsError.showError('',res.message,'error');
								}
							})
						}
					}
				})
			},
			toAdd(model) {
				const that = this
				uni.navigateTo({
					url: '/pages/mf/dh/poList?hid=' + model.id + '&carType='+ model.carType,
					events: {
						// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
						Filter(data) {

						}
					},
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						// res.eventChannel.emit('initfilter', that.filter)
					}
				})
			},
			delData(id) {
				// this.query.id = id;
				uni.showModal({
					title: '删除提示',
					content: '是否删除此计划？',
					confirmColor: '#F54E40',
					success: (res) => {
						if (!res.confirm) {
							this.isdisabled = true
							return false;
						}
						this.$u.api.mfsh.delete({
							id
						}).then(res => {
							if (res.result=="true") {
								this.$u.toast("删除成功！");
							} else {
								this.$refs.jsError.showError('',res.message,'error');
							}
							// this.query.id = ''
							// this.list = [];
							this.query.pageNo = 1;
							this.loadData();
						})
					}
				})
			},
			editDetail(model){
				const that=this
				uni.navigateTo({
				  url: '/pages/mf/dh/dhEdit?model='+JSON.stringify(model),
				  events: {
				    // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
				    Filter(data) {
						
				    }
				  },
				  success: function(res) {
				    // 通过eventChannel向被打开页面传送数据
				    // res.eventChannel.emit('initfilter', that.filter)
				  }
				})
			},
			delDetail(id) {
				// this.query.id = id;
				uni.showModal({
					title: '删除提示',
					content: '是否删除此详情？',
					confirmColor: '#F54E40',
					success: (res) => {
						if (!res.confirm) {
							this.isdisabled = true
							return false;
						}
						this.$u.api.mfsh.delDetail({
							id
						}).then(res => {
							if (res.result=="true") {
								this.$u.toast("删除成功！");
							} else {
								this.$refs.jsError.showError('',res.message,'error');
							}
							// this.query.id = ''
							// this.list = [];
							this.query.pageNo = 1;
							this.loadData();
						})
					}
				})
			},
			// this.$forceUpdate();
			reset() {
				this.list = [];
				this.query = {
					pageNo: 1,
					pageSize: 20,
				};
				this.loadData();
				this.show = false
			},
			submit() {
				this.list = [];
				this.query.pageNo = 1;
				this.loadData();
				this.show = false
			},
			startConfirm(e) {
				this.query.planDate_gte = e.year + '-' + e.month + '-' + e.day;
			},
			endConfirm(e) {
				this.query.planDate_lte = e.year + '-' + e.month + '-' + e.day;
			},
			customBack() {
				// 跳转到工作台页面
				uni.navigateBack({
					delta: 1,
				});
			},
			async calculateScrollViewHeight() {
				try {
					// 等待头部高度计算完成
					// await new Promise((resolve) => {
					//   this.$nextTick(() => {
					//     this.headerHeight = this.$refs.xmInfo.$refs['u-sticky'].height + this.$refs.xmInfo.$refs['u-sticky'].h5NavHeight + this.$refs.navbar.navbarHeight;
					//     resolve();
					//   });
					// });
					this.headerHeight = 52
					// 计算tabBar高度
					if (this.flag) {
						this.tabBarHeight = 60; // 假设tabbar高度为50px，实际应根据实际情况获取
					} else {
						this.tabBarHeight = 0;
					}
					// 获取屏幕高度
					const systemInfo = uni.getSystemInfoSync();
					const screenHeight = systemInfo.windowHeight;
					// 计算scroll-view高度
					this.scrollViewHeight = screenHeight - (this.headerHeight + this.tabBarHeight);
				} catch (error) {
					console.error('Error while calculating ScrollView height:', error);
				}
			},

			loadMore() {
				this.loadStatus = "loading";
				setTimeout(() => {
					this.query.pageNo += 1;
					this.loadData("add");
				}, 100);
			},
			loadData(type) {
				// 循环10次重复赋值
				this.$u.api.mfsh.findList(this.query).then((res) => {
					if (res.list.length >= 0) {
						// this.xmList = res.list
						if (res.list.length < 20 || res.list.length == 0) {
							this.loadStatus = "nomore";
						}
						var data = res.list;
						if (type == "add") {
							for (var i = 0; i < data.length; i++) {
								this.list.push(data[i]);
							}
						} else {
							this.list = data;
						}
					}
				});
			},
			async refresherrefresh() {
				this.triggered = true;
				this.query.pageNo = 1;
				await this.loadData();
				const scrollTop = 0
				uni.pageScrollTo({
					scrollTop,
					duration: 0,
				});
				setTimeout(() => {
					this.triggered = false;
				}, 500);
			},

			add(id) {
				if (!!id) {
					uni.navigateTo({
						url: '/pages/zfgs/proj/weekly/pmWeekly/readOnlyForm?id=' + id
					})
				} else {
					const xm = {
						projectno: this.itemContent.projectno,
						projectname: this.itemContent.projectname,
					};
					uni.navigateTo({
						url: "/pages/zfgs/proj/weekly/pmWeekly/form?xm=" + JSON.stringify(xm),
					});
				}
			},
		},
		// beforeDestroy() {
		// 	// 清理事件监听器
		// 	try {
		// 		this.$refs.xmInfo.$off('child-mounted-done');
		// 	} catch (e) {
		// 		console.log('Error while unbinding event:', e);
		// 	}
		// }
	};
</script>
<style lang="scss" scoped>
	.cu-bar {
		min-height: 60px;
	}

	.cu-btn {
		font-size: 16px;
		height: 40px;
	}

	.foot {
		background: #fff;
	}

	.cu-item {
		flex-wrap: wrap;
		padding: 8px 15px !important;
	}

	.title {
		width: 220rpx;
	}

	.cu-modal-slot {
		margin: 42rpx 32rpx 20rpx 32rpx;
		height: 100vh;
		position: relative;
	}

	.cu-modal-header {
		font-size: 38rpx;
		color: #3d3d3d;
		line-height: 40rpx;
		text-align: left;
		font-weight: 500;
	}

	.button {
		font-size: 32rpx;
		color: #666666;
		line-height: 40rpx;
		padding: 12rpx 40rpx;
		margin-bottom: 20rpx;
		background: #e2e2e2;
		border-radius: 180rpx;
	}

	.button:hover {
		background: #3e97b0;
		color: #ffffff;
	}

	.cu-modal-search-title {
		margin: 32rpx 0rpx 20rpx 0rpx;
		text-align: left;
		font-size: 32rpx;
		color: #999999;
		line-height: 40rpx;
	}

	.cu-dialog {
		background: #ffffff;
	}

	.cu-modal-footer {
		padding: 32rpx 32rpx;
		width: 100%;

		.cu-btn {
			width: 50%;
		}
	}

	.bg-confirm {
		background: #3e97b0;
		color: #ffffff;
	}
</style>